package com.ruoyi.machine.base.fitting.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.machine.base.fitting.domain.Fitting;
import com.ruoyi.machine.base.fitting.domain.FittingCond;
import com.ruoyi.machine.base.fitting.service.IFittingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 配件管理
 */
@RestController
@RequestMapping("/machine/fitting")
@Api(value = "配件管理模块", tags = "配件管理接口")
public class FittingController extends BaseController {
    @Autowired
    private IFittingService fittingService;

    /**
     * 获取配件管理
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取配件管理列表",notes = "查询所有配件管理信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody FittingCond cond) {
        startPage();
        List<Fitting> fittingList = fittingService.selectFittingList(cond);
        return getDataTable(fittingList);
    }

    /**
     * 导出配件管理
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody FittingCond cond) {
        List<Fitting> fittingList = fittingService.selectFittingList(cond);
        ExcelUtil<Fitting> util = new ExcelUtil<>(Fitting.class);
        return util.exportExcel(fittingList, "配件信息");
    }

    /**
     * 新增配件管理
     * @param fitting
     * @return
     */
    @ApiOperation(value = "新增配件管理", notes = "新增配件管理信息")
    @ApiImplicitParam(name = "fitting", value = "新增配件管理", dataType = "Fitting")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Fitting fitting) {
        Fitting fitting1 = fittingService.selectFittingByName(fitting.getFittingName(), fitting.getFittingSpecification());
        if (StringUtils.isNotNull(fitting1)) {
            //存在
            return AjaxResult.error("该规则配件已存在，请重新添加");
        }
        return toAjax(fittingService.insertFitting(fitting));
    }

    /**
     *
     * 更新配件管理
     * @param fitting
     * @return
     */
    @ApiOperation(value = "修改配件管理", notes = "修改配件管理")
    @ApiImplicitParam(name = "fitting", value = "修改配件管理", dataType = "Fitting")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Fitting fitting) {
        try {
            fittingService.updateFitting(fitting);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除配件管理
     * 仅修改删除标志位
     * @param fitting
     * @return
     */
    @ApiOperation(value = "删除配件管理", notes = "删除配件管理,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Fitting fitting) {

        return toAjax(fittingService.updateFittingDeleteFlag(fitting));
    }
}
